package cn.taobaoke.restful.service;

import cn.taobaoke.restful.domain.PIDEntity;
import cn.taobaoke.restful.domain.PIDRepository;
import cn.taobaoke.restful.exception.TBKException;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author yangzuo
 * @since 2017-04-06
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PIDService {

    public void createPids(final List<String> pids, final Long domainId) {

        val pidEntityList = new ArrayList<PIDEntity>();
        for (String pid : pids) {
            val opt = pidRepository.findByPidAndDomainId(pid, domainId);
            if (! opt.isPresent()) { // 不存在插入
                pidEntityList.add(new PIDEntity(pid, false, domainId));
            }
        }
        pidRepository.saveAll(pidEntityList);
    }

    public PIDEntity getFirstAvailablePID(final Long domainId) {
        val pidEntityOpt = pidRepository.findFirstByIsAssignedAndDomainId(false, domainId);
        if (! pidEntityOpt.isPresent()) {
            throw new TBKException("没有可用的推广位");
        }
        return pidEntityOpt.get();
    }

    public PIDEntity savePid(final String pid, final String account, final Long domainId) {
        val entityOpt = pidRepository.findByPidAndDomainId(pid, domainId);
        if (entityOpt.isPresent()) {
            throw new TBKException("推广位重复");
        }
        PIDEntity pidEntity = new PIDEntity();
        pidEntity.setPid(pid);
        pidEntity.setIsAssigned(true);
        pidEntity.setAccount(account);
        pidEntity.setDomainId(domainId);
        return pidRepository.save(pidEntity);
    }

    public PIDEntity savePidFromAlimamaRobot(final String pid, final String account, final Long domainId) {
        val entityOpt = pidRepository.findByPidAndDomainId(pid, domainId);
        if (entityOpt.isPresent()) {
            throw new TBKException("推广位重复");
        }
        PIDEntity pidEntity = new PIDEntity();
        pidEntity.setPid(pid);
        pidEntity.setIsAssigned(false);
        pidEntity.setAccount(account);
        pidEntity.setDomainId(domainId);
        return pidRepository.save(pidEntity);
    }

    public void saveOrCreatePid(final String newPid, final String account, final Long agentId, final String oldPid, final Long domainId) {
        val newPidEntityOpt = pidRepository.findByPidAndDomainId(newPid, domainId);
        val oldPidEntityOpt = pidRepository.findByPidAndDomainId(oldPid, domainId);

        PIDEntity newPidEntity = null;
        PIDEntity oldPidEntity = null;

        if (newPidEntityOpt.isPresent()) {
            newPidEntity = newPidEntityOpt.get();
            newPidEntity.setAccount(account);
            newPidEntity.setAgentId(agentId);
            newPidEntity.setIsAssigned(true);
        } else {
            newPidEntity = new PIDEntity();
            newPidEntity.setPid(newPid);
            newPidEntity.setIsAssigned(true);
            newPidEntity.setAccount(account);
            newPidEntity.setAgentId(agentId);
            newPidEntity.setDomainId(domainId); // important
        }
        if (oldPidEntityOpt.isPresent()) { // 将已分配的pid回收
            oldPidEntity.setIsAssigned(false);
            oldPidEntity.setAgentId(null);
        }
        pidRepository.saveAll(Arrays.asList(newPidEntity, oldPidEntity));
    }

    public PIDEntity savePid(final String pid, final String account, final Long agentId, final Long domainId) {
        PIDEntity pidEntity = new PIDEntity();
        pidEntity.setPid(pid);
        pidEntity.setAccount(account);
        pidEntity.setIsAssigned(true);
        pidEntity.setAgentId(agentId);
        pidEntity.setDomainId(domainId);
        return pidRepository.save(pidEntity);
    }

    public PIDEntity getPidByAgentId(final Long agentId, final Long domainId) {
        List<PIDEntity> pidEntityList = pidRepository.findByAgentIdAndDomainId(agentId, domainId);
        if (pidEntityList.size() > 0) {
            return pidEntityList.get(0);
        }
        return null;
    }

    public PIDEntity getOrCreateAdminPid(final Long domainId) {
        List<PIDEntity> pidEntities = pidRepository.findAdminPid(domainId);
        if (pidEntities != null && pidEntities.size() > 0) return pidEntities.get(0);

        PIDEntity pidEntity = getFirstAvailablePID(domainId);
        if (pidEntity == null) {
            throw new TBKException("推广位PID已经不足,请开启阿里妈妈机器人再创建新的PID");
        }

        pidEntity.setIsAssigned(true);
        return pidRepository.save(pidEntity);
    }

    public PIDEntity updatePID(PIDEntity pidEntity) {
        return pidRepository.save(pidEntity);
    }

    private final @NonNull PIDRepository pidRepository;
}
